package root.electronclass.electronclass.mvp.base

import root.electronclass.electronclass.mvp.entity.ApiException

/**
 *
 * @author Lokiy
 * @date 2023/3/24 22:01
 */

val BaseResponse<*>?.isSuccessful
    get() = this != null && this.code == 200 && this.rows != null

val BaseResponse<*>?.isActionSuccessful
    get() = this != null && this.code == 200

val <T> BaseResponse<T>?.data: T
    get() = if (isSuccessful) this!!.rows else check().rows

private fun <T> BaseResponse<T>?.check() :BaseResponse<T> {
    if (this == null) {
        throw IllegalArgumentException("返回对象为空")
    }
    if (!isSuccessful) {
        throw ApiException(code, msg)
    }
    return this
}